#include "mouse.h"
#include "commonFunc.h"

mouse::mouse()
{}

mouse::mouse(int nw, int nh, int bw, int bh, int nx, int ny, bool nf)
{
    width = nw;
    height = nh;
    blockwidth = bw;
    blockheight = bh;
    x = nx;
    y = ny;
    filled = nf; //Determines whether its a hollow square or filled in

    //Make a multi dimensional array of booleans, true = walkable, false = not walkable
    walkgrid = new bool*[height];
    for (int i = 0; i < height; i++)
    {
	walkgrid[i] = new bool[width];
	for (int z = 0; z < width; z++)
	{
	    walkgrid[i][z] = true;
	}
    }

    //Set the start and end location as not buildable
    walkgrid[0][0] = false;
    walkgrid[height-1][width-1] = false;
}

mouse::~mouse()
{
    for (int i = 0; i < height; i++)
    {
	delete [] walkgrid[i];
    }
    delete [] walkgrid;
}

void mouse::draw()
{
    glPushMatrix();
    glTranslatef(x*blockwidth,y*blockheight,0);
    glDisable(GL_TEXTURE_2D);
    


    for (int dx = 0; dx <= 1; dx++)
    {
	for (int dy = 0; dy <= 1; dy++)
	{
	    if (filled)
		glBegin(GL_QUADS);
	    else
		glBegin(GL_LINE_LOOP);
	    
	    if (walkgrid[y+dy][x+dx] == true)
		glColor4f(0,1,0,0.5);
	    else
		glColor4f(1,0,0,0.5);
	    //glColor4f(1,1,1,1);

	    
	    glVertex3f(dx*blockwidth,dy*blockheight,0);
	    glVertex3f((1+dx)*blockwidth, dy*blockheight,0);
	    glVertex3f((1+dx)*blockwidth, (1+dy)*blockheight,0);
	    glVertex3f(dx*blockwidth,(1+dy)*blockheight,0);
	    glEnd();
	    
	}
    }
    



    
    //glColor4f(1,1,1,1);
    glEnable(GL_TEXTURE_2D);
    glPopMatrix();
    glLoadIdentity();
    glColor4f(1,1,1,1);
}
void mouse::move(int dx, int dy)
{
    //Move mouse with delta x and delta y if it is within the bounds of the grid
    if (x+dx >= 0 && x+dx+1 < width)
    {
	x += dx;
    }
    if (y+dy >= 0 && y+dy+1 < height)
    {
	y += dy;
    }
}
bool mouse::canbuild()
{
    //Find out if it is buildable
    for (int dx = 0; dx <= 1; dx++) //Test mouse spot and 1 over
    {
	for (int dy = 0; dy <= 1; dy++) // test mouse spot and 1 down
	{
	    if (!walkgrid[y+dy][x+dx]) // if not true at spot return false
	    {
		return false;
	    }
	}
    }
    return true;
}
